package InterFaceContact;

import interfaceGrafica.Acoes;
import interfaceGrafica.AdicionarGrafo;
import interfaceGrafica.AdicionarLinha;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class AbrirProjetoDeSimulacao {
	public static void atualizarProjeto(String path) throws IOException {
		
		Position.setPos(new HashMap<String, Double[]>());
		Position.setLig(new HashMap<String, List<String>>());
		Acoes.removeCells();
		ReadData.readFromFile(path);
		posicionaNos(Position.pos);
		posicionaLinks(Position.lig);
		System.out.println("Topologia carregada com sucesso!");
	}

	private static void posicionaNos(HashMap<String, Double[]> pos) {
		Iterator<String> it = pos.keySet().iterator();
		String key = new String();
		Double posX, posY;
		
		while(it.hasNext()){
			key = it.next();
			posX = pos.get(key)[0];
			posY = pos.get(key)[1];
			
			AdicionarGrafo add = new AdicionarGrafo(key, posX, posY);
		}
	}
	
	private static void posicionaLinks(HashMap<String, List<String>> lig) {
		Iterator<String> it1 = lig.keySet().iterator();
		HashMap<String, List<String>> temp = new HashMap<String, List<String>>();
		Iterator<String> it2;
		String or = new String();
		String des = new String();
		
		while(it1.hasNext()){
			or = it1.next();
			it2 = lig.get(or).iterator();
			while(it2.hasNext()){
				des = it2.next();
				
				if(!temp.containsKey(or)){
					temp.put(or, new ArrayList<String>());
				}
				if(!temp.containsKey(des)){
					temp.put(des, new ArrayList<String>());
				}
				
				if (!temp.get(or).contains(des)){
					temp.get(or).add(des);
					temp.get(des).add(or);
					AdicionarLinha addLinha2 = new AdicionarLinha(or, des, Acoes.topologia.get(or).get(des).dist);
				}
			}
		}
	}
	
}
